#include <stdio.h>
#include <windows.h>

#include "config.h"
#include "event.h"
#include "util.h"

int
main(int argc, char **argv)
{
	FILE *fpcfg, *fplog;
	char *cfg_file, *log_file;
	int r;
	struct config config;
	uint32_t addr, next;
	int sessions;

	if (argc != 3) {
		fprintf(stderr, "usage: %s config_file log_file\n",
		    argv[0]);
		exit(1);
	}

	cfg_file = argv[1];
	log_file = argv[2];

	fpcfg = fopen(cfg_file, "rb");
	if (fpcfg == NULL) {
		fprintf(stderr, "error: could not open %s\n", cfg_file);
		exit(1);
	}

	r = fread(&config, 1, sizeof(config), fpcfg);

	if (!valid_config(&config)) {
		fprintf(stderr, "error: invalid config read.\n");
		exit(1);
	}
	
	fplog = fopen(log_file, "rb");
	if (fplog == NULL) {
		fprintf(stderr, "error: could not open %s\n", log_file);
		exit(1);
	}

	addr = 0;
	sessions = 0;
	while (addr < config.nvm_start_addr) {
		r = fread(&next, 1, sizeof(next), fplog);
		if (r != sizeof(next)) {
			fprintf(stderr, "error: could not read next"
			    " address\n");
			exit(1);
		}
		addr += r;

		if (fseek(fplog, next, SEEK_SET) == -1) {
			fprintf(stderr, "error: could not seek next"
			    " address\n");
			exit(1);
		}

		printf("Session %d: %d bytes.\n", sessions, next - addr);
		addr = next;
		++sessions;
	}

	fclose(fpcfg);
	fclose(fplog);

	return 0;
}
